package com.xbongbong.rule.symbol.base;

import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.rule.rules.XbbCondition;
import com.xbongbong.rule.symbol.AllIncludeSymbol;
import com.xbongbong.rule.symbol.DynamicSymbol;
import com.xbongbong.rule.symbol.EmptySymbol;
import com.xbongbong.rule.symbol.EqualSymbol;
import com.xbongbong.rule.symbol.InSymbol;
import com.xbongbong.rule.symbol.IncludeSymbol;
import com.xbongbong.rule.symbol.LikeSymbol;
import com.xbongbong.rule.symbol.NoInSymbol;
import com.xbongbong.rule.symbol.NoLikeSymbol;
import com.xbongbong.rule.symbol.NotEmptySymbol;
import com.xbongbong.rule.symbol.NotEqualSymbol;
import com.xbongbong.rule.symbol.NumberCompareSymbol;
import com.xbongbong.rule.symbol.RangeSymbol;


/**
 * @author zheng.li
 **/
public class SymbolContext {

    private XbbCondition item;

    private Object factValue;

    /**
     * 符号运算类
     */
    private ISymbol symbol;

    public SymbolContext() {

    }

    public SymbolContext(XbbCondition xbbCondition,Object factValue) {
        this.item = xbbCondition;
        this.factValue = factValue;
    }

    public void setItem(XbbCondition item) {
        this.item = item;
    }

    public void setJson(Object factValue) {
        this.factValue = factValue;
    }

    /**
     * 获取最终运算执行类
     */
    private void getWorker() {
        ConditionEnum conditionEnum = item.getConditionEnum();
        if (conditionEnum == null) {
            return;
        }

        switch (conditionEnum) {
            case EQUAL:
                symbol =  new EqualSymbol();
                break;
            case NOEQUAL:
                symbol = new NotEqualSymbol();
                break;
            case EMPTY:
                symbol = new EmptySymbol();
                break;
            case NOEMPTY:
                symbol = new NotEmptySymbol();
                break;
            case LESSTHAN:
            case LESSEQUAL:
            case GREATERTHAN:
            case GREATEREQUAL:
                symbol = new NumberCompareSymbol();
                break;
            case ALLINCLUDE:
                symbol = new AllIncludeSymbol();
                break;

            case INCLUDE:
                symbol = new IncludeSymbol();
                break;
            case LIKE:
                symbol = new LikeSymbol();
                break;
            case NOLIKE:
                symbol = new NoLikeSymbol();
                break;
            case IN:
                symbol = new InSymbol();
                break;
            case NOIN:
                symbol = new NoInSymbol();
                break;
            case RANGE:
                symbol = new RangeSymbol();
                break;
            case DYNAMIC:
                symbol = new DynamicSymbol();
                break;
            default:
                break;
        }
    }

    /**
     * 运算结果
     * @return boolean
     */
    public boolean operation() {

       getWorker();
       if (symbol == null) {
           return false;
       }
       symbol.parse(item,factValue);
       return symbol.operation();
    }


}
